﻿using System;
using System.Linq;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.Interfaces;
using iss.sg4210.se18.team6s.jass.DAL.DataAccess.MSSQL;

namespace iss.sg4210.se18.team6s.jass.BL
{
    public class ResponsesFacade
    {
        IResponse responseRepo = null;
        IResponseActions responseActionRepo = null;
        IActions actionRepo = null;
        public ResponsesFacade()
        {
            responseRepo = new ResponseDAO();
            responseActionRepo = new ResponseActionsDAO();
            actionRepo = new ActionsDAO();
        }

        #region Response
        public void AddResponse(Response response)
        {
            //check if the person has already responded
            if (HasAlreadyResponded(response))
                throw new Exception("User had already responded to this Job Advertisement!");

            //then add
            responseRepo.AddResponse(response);
        }

        public Response GetResponseById(int id)
        {
            return responseRepo.GetResponse(id);
        }

        public IQueryable<Response> FindAllResponsesByUser(Guid userId)
        {
            return responseRepo.FindAllResponsesByUser(userId);
        }

        public void UpdateResponse(Response response)
        {
            responseRepo.UpdateResponse(response);
        }

        private bool HasAlreadyResponded(Response response)
        {
            return responseRepo.FindAllResponsesByUser(response.UserId).Any(m => m.AdvertisementId == response.AdvertisementId);
        }

        public void DeleteResponse(int id)
        {
            responseRepo.DeleteResponse(id);
        }
        #endregion

        #region ResponseAction
        public IQueryable<ResponseAction> FindAllResponseActions()
        {
            return responseActionRepo.FindAllResponseActions();
        }

        public void DeleteResponseAction(int id)
        {
            responseActionRepo.DeleteResponseAction(id);
        }

        public ResponseAction GetResponseAction(int id)
        {
            return responseActionRepo.GetResponseAction(id);
        }

        public void UpdateResponseAction(ResponseAction responseAction)
        {
            responseActionRepo.UpdateResponseAction(responseAction);
        }

        public void AddResponseAction(ResponseAction responseAction)
        {
            responseActionRepo.AddResponseAction(responseAction);
        }
        #endregion

        #region Action
        public void DeleteAction(int id)
        {
            actionRepo.DeleteAction(id);
        }

        public DAL.Action GetAction(int id)
        {
            return actionRepo.GetAction(id);
        }

        public void UpdateAction(DAL.Action action)
        {
            actionRepo.UpdateAction(action);
        }

        public void AddAction(DAL.Action action)
        {
            actionRepo.AddAction(action);
        }

        public IQueryable<DAL.Action> FindAllActions()
        {
            return actionRepo.FindAllActions();
        }
        #endregion
    }
}
